//
//  QRParser.m
//  QRCodeManager
//
//  Created by Cosmin Stirbu on 4/17/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "QRParser.h"

@implementation QRParser

- (id)initWithData:(NSString *)qrData {
    self = [super init];
    if (self) {
        _qrData = qrData;
    }
    return self;
}


- (NSString *)valueForRegEx:(NSString *)regEx withOffset:(NSInteger)offSet trimLength:(NSInteger)length {
    NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regEx
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:NULL];
    NSArray *matches = [re matchesInString:_qrData
                                   options:0
                                     range:NSMakeRange(0, _qrData.length)];
    if ([matches count] != 1) {
        return nil;
    }
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    NSRange range = NSMakeRange(match.range.location + offSet, match.range.length - offSet);
    NSString *string =  [_qrData substringWithRange:range];
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return [string substringToIndex:[string length] - length];
}

@end
